import { getUserProfile, getUserProfileById } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  // 公共数据
  state: {
    // 本地取一下token
    token: getToken() || '',
    userInfo: {}

  },
  mutations: {
    // 设置token
    setToken(state, Token) {
      state.token = Token

      // 本地存储token
      setToken(Token)
    },
    // 删除token
    removeToken(state) {
      state.token = ''
      // 删除本地token
      removeToken()
    },
    updateUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async  getUserInfo(context) {
      try {
        const res = await getUserProfile()
        console.log(res.data, 'res')
        const info = await getUserProfileById(res.data.userId)
        console.log(info.data, 'info')
        context.commit('updateUserInfo', { ...res.data, ...info.data })
      } catch (error) {
        console.log(error)
      }
    }
  }
}
